package com.live.adapter;

import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleAdapter;

import com.live.activity.R;
import com.live.common.Constant;
import com.live.common.LiveImageHelper;
import com.live.task.LiveReview;

public class LiveReviewSimpleAdapter extends SimpleAdapter {

	public List<Map<String, Object>> reviewList = null;
	public Context context = null;
	public String userId = null;
	public int index = 0;

	private LiveReview reviewTask = null;

	public LiveReviewSimpleAdapter(Context context, List<Map<String, Object>> items, int resource, String[] from,
			int[] to, String userId) {
		super(context, items, resource, from, to);
		this.reviewList = items;
		this.context = context;
		this.userId = userId;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = super.getView(position, convertView, parent);

		Map<String, Object> review = reviewList.get(position);

		// 设置头像
		ImageView reviewUserPic = (ImageView) convertView.findViewById(R.id.reviewUserPic);
		if (Constant.Y.equals(review.get("reviewUserPic"))) {
			LiveImageHelper image = new LiveImageHelper(reviewUserPic);
			image.execute(Constant.SITE_URL + Constant.SITE_AVATAR_PATH + review.get("id") + ".jpg");
		} else {
			reviewUserPic.setImageResource(R.drawable.none);
		}

		// 设置删除按钮
		ImageView reviewDelete = (ImageView) convertView.findViewById(R.id.reviewDelete);
		if (review.get("reviewUser").equals(userId)) {

			reviewDelete.setVisibility(View.VISIBLE);
			reviewDelete.setTag(position);

			// 删除评论事件监听
			reviewDelete.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {

					// 取得要删除对象索引
					index = Integer.parseInt(v.getTag().toString());

					// 生成确认对话框
					AlertDialog.Builder builder = new AlertDialog.Builder(context);
					builder.setTitle(R.string.delete_confirm_info);
					builder.setIcon(android.R.drawable.ic_dialog_info);
					builder.setPositiveButton(R.string.delete_btn, new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {

							String url = Constant.SITE_URL + "live_review_del.php?reviewId="
									+ reviewList.get(index).get("reviewId");

							// 删除评论
							reviewTask = new LiveReview(context, null);

							// 如果删除成功
							if (reviewTask.delMatchReview(url)) {
								// 刷新评论列表
								reviewList.remove(index);
								LiveReviewSimpleAdapter.this.notifyDataSetChanged();
							}

						}
					});
					builder.setNegativeButton(R.string.cancel_btn, new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
						}
					});
					builder.create().show();

				}
			});
		} else {
			reviewDelete.setVisibility(View.INVISIBLE);
		}
		return convertView;
	}
}